package com.harmontronics.hanma.paas.paas.domain.app.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.Fields;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.I18nKeyEnum;
import com.harmontronics.hanma.paas.paas.domain.app.repository.persistence.HmFolderRepository;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmFolder;
import com.harmontronics.hanma.paas.paas.domain.app.repository.persistence.HmProcessRepository;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.app.repository.mapper.HmFolderMapper;
import com.harmontronics.hanma.paas.paas.domain.app.repository.po.HmFolderPO;

import java.util.List;

/**
 * @author ldb
 */
@Service("hmFolderService")
public class HmFolderService {

    @Autowired
    private HmFolderRepository hmFolderRepository;

    @Autowired
    private HmProcessRepository hmProcessRepository;


    public HmFolder findById(Long id) {
        HmFolderPO po = hmFolderRepository.getById(id);
        return BeanUtil.toBean(po, HmFolder.class);
    }

    public void save(HmFolder hmFolder) {
        validField(null, Fields.NAME, hmFolder.getName());
        HmFolderPO po = BeanUtil.toBean(hmFolder, HmFolderPO.class);
        hmFolderRepository.save(po);
    }

    public void update(HmFolder hmFolder) {
        validField(hmFolder.getId(), Fields.NAME, hmFolder.getName());
        HmFolderPO po = hmFolderRepository.getById(hmFolder.getId());
        po.setName(hmFolder.getName());
        po.setDescription(hmFolder.getDescription());
        hmFolderRepository.updateById(po);
    }

    public void remove(List<Long> ids) {
        for (Long id : ids) {
            int count = hmProcessRepository.countByFolderId(id);
            if (count == 0) {
                hmFolderRepository.removeById(id);
            } else {
                throw new ErrorKeyException(I18nKeyEnum.FOLDER_HAS_PROCESS);
            }
        }

    }

    public List<HmFolder> getList() {
        List<HmFolderPO> pos = hmFolderRepository.list();
        return Convert.toList(HmFolder.class, pos);
    }

    public PageBase<HmFolder> getList(int pageNo, int pageSize) {
        Page<HmFolderPO> page = new Page<>(pageNo, pageSize);
        IPage<HmFolderPO> resultPage =
                hmFolderRepository.selectPage(Wrappers.emptyWrapper(), page);
        return PageBase.from(resultPage, HmFolder.class);
    }


    public void validField(Long id, String field, String value) {
        if (Fields.NAME.equals(field)) {
            HmFolderPO hmFolderPo = hmFolderRepository.findByName(value);
            if (hmFolderPo == null) {
                return;
            }
            if (id == null) {
                throw new ErrorKeyException(I18nKeyEnum.FOLDER_NAME_EXISTS);
            }
            if (hmFolderPo.getId().longValue() != id.longValue()) {
                throw new ErrorKeyException(I18nKeyEnum.FOLDER_NAME_EXISTS);
            }
        }
    }

}